package ui.forms;

import base.ArrayOfFood;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.list.ListModel;
import java.rmi.RemoteException;
import java.util.Vector;
import stub.FoodServicesSoap_Stub;

/**
 *
 * @author Shayan, Meera
 */
class FilterProxyListModel implements ListModel, DataChangedListener {

    private ListModel underlying;
    private Vector filter;
    private Vector listeners = new Vector();
    private FoodServicesSoap_Stub foodServices;

    public FilterProxyListModel(ListModel underlying) {
        this.underlying = underlying;
        foodServices = new FoodServicesSoap_Stub();
        underlying.addDataChangedListener(this);
    }

    private int getFilterOffset(int index) {
        if (filter == null) {
            return index;
        }
        if (filter.size() > index) {
            return ((Integer) filter.elementAt(index)).intValue();
        }
        return -1;
    }

    private int getUnderlyingOffset(int index) {
        if (filter == null) {
            return index;
        }
        return filter.indexOf(new Integer(index));
    }

    private String currentFilter;
    public synchronized  void filter(String str) throws RemoteException {

        if (str.equalsIgnoreCase(currentFilter))
            return;
        
        for (int i = underlying.getSize() - 1; i >= 0; i--) {
            underlying.removeItem(i);
        }
        ArrayOfFood foods = foodServices.getFoodsWithSimilarNames(str);
        for (int i = 0; i < foods.getFoods().length; i++) {
            underlying.addItem(foods.getFoods()[i]);
        }

        currentFilter = str;
        dataChanged(DataChangedListener.CHANGED, -1);
    }

    public Object getItemAt(int index) {
        return underlying.getItemAt(getFilterOffset(index));
    }

    public int getSize() {
        if (filter == null) {
            return underlying.getSize();
        }
        return filter.size();
    }

    public int getSelectedIndex() {
        return Math.max(0, getUnderlyingOffset(underlying.getSelectedIndex()));
    }

    public void setSelectedIndex(int index) {
        underlying.setSelectedIndex(getFilterOffset(index));
    }

    public void addDataChangedListener(DataChangedListener l) {
        listeners.addElement(l);
    }

    public void removeDataChangedListener(DataChangedListener l) {
        listeners.removeElement(l);
    }

    public void addSelectionListener(SelectionListener l) {
        underlying.addSelectionListener(l);
    }

    public void removeSelectionListener(SelectionListener l) {
        underlying.removeSelectionListener(l);
    }

    public void addItem(Object item) {
        underlying.addItem(item);
    }

    public void removeItem(int index) {
        underlying.removeItem(index);
    }

    public void dataChanged(int type, int index) {
        if (index > -1) {
            index = getUnderlyingOffset(index);
            if (index < 0) {
                return;
            }
        }
        for (int iter = 0; iter < listeners.size(); iter++) {

            ((DataChangedListener) listeners.elementAt(iter)).dataChanged(type, index);
        }
    }
}
